<?php

namespace app\data\model;

use think\Model;

class BuyerModel extends Model
{
    /* 自动处理时间 */
    protected $autoWriteTimestamp = 'int';

    public function getCount($users = '', $start_time = 0, $end_time = 0)
    {
        $this->alias('lists');
        if ($users) {
            $this->join('__BUYER_USER__ muser', 'lists.id = muser.pid and muser.level = 1');
            $this->join('__BUYER_USER__ auser', 'lists.id = auser.pid and auser.level = 2');
            $this->whereIn('muser.uid|auser.uid', $users);
        }
        if ($end_time) {
            $this->whereBetween('lists.createtime', [$start_time, $end_time]);
        }
        $this->field([
            'count(lists.id) as `count`'
        ]);
        $result = $this->find();
        return $result ? $result->toArray() : [];
    }

    public function getCate($users = '', $start_time = 0, $end_time = 0)
    {
        $this->alias('lists');
        $this->join('__INDUSTRY_TYPE__ type', 'lists.id = type.pid', 'right');
        $this->join('__INDUSTRY_CATE__ cate', 'type.cate_id = cate.id');
        if ($users) {
            $this->join('__BUYER_USER__ muser', 'lists.id = muser.pid and muser.level = 1');
            $this->join('__BUYER_USER__ auser', 'lists.id = auser.pid and auser.level = 2');
            $this->whereIn('muser.uid|auser.uid', $users);
        }
        if ($end_time) {
            $this->whereBetween('lists.createtime', [$start_time, $end_time]);
        }
        $this->field([
            'count(lists.id) as `count`,cate.name'
        ]);
        $this->group('cate.id');
        $result = $this->select();
        return $result ? $result->toArray() : [];
    }

    public function getStaff($users = '', $start_time = 0, $end_time = 0)
    {
        $this->alias('lists');
        $this->join('__BUYER_USER__ muser', 'lists.id = muser.pid and muser.level = 1');
        $this->join('__USER__ user', 'user.id = muser.uid', 'right');
        if ($users) {
            $this->whereIn('user.id', $users);
        }
        if ($end_time) {
            $count = "count(if(lists.createtime >= {$start_time} and lists.createtime <= {$end_time},lists.id,null)) as `count`,user.user_nickname as name,user.id as uid";
        } else {
            $count = 'count(lists.id) as `count`,user.user_nickname as name,user.id as uid';
        }
        $this->where(['user.user_status' => [
            'gt', 0
        ]]);
        $this->where(['user.id' => [
            'neq', 1
        ]]);
        $this->field($count);
        $this->group('user.id');
        $result = $this->select();
        return $result ? $result->toArray() : [];
    }

    public function getCateFollow($users = '', $start_time = 0, $end_time = 0)
    {
        $this->alias('lists');
        $this->join('__INDUSTRY_TYPE__ type', 'lists.id = type.pid','right');
        $this->join('__FOLLOW__ follow', 'lists.id = follow.pid and follow.type = 2', 'right');
        $this->join('__INDUSTRY_CATE__ cate', 'type.cate_id = cate.id');
        if ($users) {
            $this->whereIn('follow.uid', $users);
        }
        if ($end_time) {
            $count = "count(if(lists.createtime >= {$start_time} and lists.createtime <= {$end_time},lists.id,null)) as `count`,user.user_nickname as name,user.id as uid";
        } else {
            $count = 'count(cate.id) as `count`,cate.name';
        }
        $this->field($count);
        $this->group('type.cate_id');
        $result = $this->select();
        return $result ? $result->toArray() : [];
    }
}